import com.blog.domain.Favorites;
import com.blog.itpub.HtmlUtils;
import com.blog.itpub.SimpleCrawler;
import com.blog.search.UpdateIndexService;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class ImportBookMarket {

	public static void main(String[] args) throws IOException, URISyntaxException, SQLException {

		UpdateIndexService.setIndexDir("E:\\iworkspace\\blog\\src\\main\\webapp\\index");
		Set<String> urls = new HashSet<String>();
		File file = new File(ImportBookMarket.class.getClassLoader().getResource("bookmarks.html").toURI());
		Document doc = Jsoup.parse(file,"UTF-8");

		Elements as = doc.select("A");
		for( Element e : as ){
			String url = e.attr("HREF");
			String title = e.text();
			String time = e.attr("ADD_DATE");

			System.out.println("url=" + url);

			try{
				Pair<Integer,String> ret = SimpleCrawler.get(e.attr("HREF"), Charset.forName("UTF-8"));
				String title2 = HtmlUtils.title(ret.getRight());
				if( title2 != null && title2.length() > 0 ){
					title = title2;
				}
			}catch (Exception ex){
				System.err.println(e.attr("HREF"));
			}

			Favorites f = new Favorites();
			f.setAuthor("zhangya");
			f.setFname(title);
			f.setFdescription(title);
			f.setFlink(url);
			f.setOps(0);
			f.setTime(time(time));

			try{
				if( !urls.contains(f.getFlink()) ){
					f.insert();
					//UpdateIndexService.add(f);
					urls.add(f.getFlink());
				}
			}catch (SQLException ex){
				ex.printStackTrace();
				//ingnore
			}
		}

	}

	private static String time(String time){
		long l = 0;
		try{
			l = Long.parseLong(time);
		}catch (Exception e){
			e.printStackTrace();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(l));
	}

}
